/**
 * Created by subo on 16/4/13.
 */

function Status(code, desc) {
	this.code = code;
	this.desc = desc;
}

function getJson(url, callback) {
	var res, err;
	res = {};
	$.ajax({
		url: url,
		type: 'GET',
		dataType: 'json',
		success: function(data) {
			res = data;
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			err = errorThrown;
		},
		complete: function(XMLHttpRequest, textStatus) {
			res.status = new Status(err?-1:0, textStatus + (err?': ' + err:''));
			callback(res);
		}
	});
}

var TEST_JSON_DATA_PATH = '/YYK_shop_frontend/tests/json';
function listBrands(callback) {
	getJson(TEST_JSON_DATA_PATH + '/brand/brandList.json', function(res){
		callback(res);
	})
}

function listCategories(callback) {
	getJson(TEST_JSON_DATA_PATH + '/category/categoryList.json', function(res){
		callback(res);
	})
}

function listProducts(callback) {
	getJson(TEST_JSON_DATA_PATH + '/product/productList.json', function(res){
		callback(res);
	})
}

function getProduct(productId, callback) {
	getJson(TEST_JSON_DATA_PATH + '/product/product.json', function(res){
		callback(res);
	})
}

function getPromotion(promotionId, callback) {
	getJson(TEST_JSON_DATA_PATH + '/promotion/promotion.json', function(res){
		callback(res);
	})
}

function listShops(callback) {
	getJson(TEST_JSON_DATA_PATH + '/shop/shopList.json', function(res){
		callback(res);
	})
}

function getShop(shopId, callback) {
	getJson(TEST_JSON_DATA_PATH + '/shop/shop.json', function(res){
		callback(res);
	})
}
